import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jet_rails/jr_app/jr_webview/jr_webview.dart';
import 'package:jet_rails/jr_constant/jr_urls.dart';
import 'package:jet_rails/jr_services/jr_services.dart';
import 'package:jet_rails/jr_services/jr_tracking.dart';
import 'package:jet_rails/jr_utils/jr_logger.dart';
import 'package:jet_rails/jr_utils/jr_toast.dart';

class JrSnippets {
  static void tapTermOfUse() {
    JrTracking.click(JrTrackingPages.pageTerms);
    Get.dialog(const JrWebView(url: JrUrls.termConditions), useSafeArea: false);
  }

  static void tapPrivacyPolicy() {
    JrTracking.click(JrTrackingPages.pagePrivacy);
    Get.dialog(const JrWebView(url: JrUrls.privacyPolicy), useSafeArea: false);
  }

  static const commonGradient = LinearGradient(
    begin: Alignment.bottomLeft, // start at the bottom left
    end: Alignment.topRight, // end at the top right
    colors: [
      Color(0xFF683F68), // left bottom corner color
      Color(0xFF9248B7), // right top corner color
    ],
  );

  static const commonTextStyle = TextStyle(height: 1, color: Colors.white, fontFamily: 'SourceHan');

  static void popMoreMenuDialog(String userId) {
    final isFollowed = JrServices.instance.isFollowed(userId);

    List menuList = [
      {
        'title': isFollowed ? 'Unfollow' : 'Follow',
        'onPressed': () async {
          try {
            EasyLoading.show();
            if (isFollowed) {
              await JrServices.instance.unFollow(userId);
              JrToast.show('Unfollow Successfully');
            } else {
              await JrServices.instance.follow(userId);
              JrToast.show('Follow Successfully');
            }
          } catch (e, s) {
            JrLogger.e('$e  $s');
          } finally {
            EasyLoading.dismiss();
          }
        },
      },
      {
        'title': 'Block',
        'onPressed': () async {
          try {
            EasyLoading.show();
            JrTracking.click(JrTrackingPages.pageBlock);
            await JrServices.instance.blockUser(userId);
            JrToast.show('Block Successfully');
            Get.back();
          } finally {
            EasyLoading.dismiss();
          }
        },
      },
      {
        'title': 'Report',
        'onPressed': () async {
          popReportDialog(userId);
        }
      }
    ];
    showCupertinoModalPopup(
      context: Get.context!,
      builder: (context) {
        List<Widget> children = [];
        for (var item in menuList) {
          children.add(CupertinoActionSheetAction(
            onPressed: () async {
              Navigator.pop(context);
              item['onPressed']?.call();
            },
            child: Text(item['title']),
          ));
        }

        return CupertinoActionSheet(
          actions: children,
          cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Cancel'),
          ),
        );
      },
    );
  }

  static void popReportDialog(String userId) async {
    List reportList = ['Pornographic', 'False gender', 'Fraud', 'Political sensitive', 'Other'];
    showCupertinoModalPopup(
      context: Get.context!,
      builder: (context) {
        return CupertinoActionSheet(
          actions: <Widget>[
            ...reportList
                .map((e) => CupertinoActionSheetAction(
                      onPressed: () async {
                        JrTracking.click(JrTrackingPages.pageReport);
                        try {
                          EasyLoading.show();
                          await JrServices.instance.report(userId, e);
                          await Future.delayed(const Duration(seconds: 3));
                          JrToast.show('Report Successfully');
                          Navigator.pop(context);
                        } catch (e) {
                          JrToast.show('Report Failed');
                        } finally {
                          EasyLoading.dismiss();
                        }
                      },
                      child: Text(e),
                    ))
                .toList(),
          ],
          cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Cancel'),
          ),
        );
      },
    );
  }
}
